package spatial.action;

import java.io.DataOutputStream;
import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;

import services.SpatialService;

import com.opensymphony.xwork2.ActionSupport;

public class SpatialAction extends ActionSupport implements ServletResponseAware{
	
	private static final long serialVersionUID = 1L;
	private double lat;
	private double lng;
	
	private SpatialService service = new SpatialService();
	private HttpServletResponse response;
	
	public void gerarAcudesKml() throws Exception{
		writeResponse(this.service.gerarAcudesKml());
	}
	
	public void gerarBufferKml() throws SQLException{
		writeResponse(this.service.gerarBufferKml());
	}
	
	public void gerarAcudeSelectedKml() throws SQLException{
		writeResponse(this.service.gerarAcudeSelectedKml(lat,lng));
	}
	
	private void writeResponse(String xml){
		response.setContentType("application/xml");
		DataOutputStream dos = null;
		try {
			dos = new DataOutputStream(response.getOutputStream());
			dos.write(xml.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(dos != null) {
				try {
					dos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Override
	public void setServletResponse(HttpServletResponse resp) {
		this.response = resp;
	}

	public double getLat() {
		return lat;
	}

	public void setLat(double lat) {
		this.lat = lat;
	}

	public double getLng() {
		return lng;
	}

	public void setLng(double lng) {
		this.lng = lng;
	}
}
